package com.fang.offer;

/**
 * <pre>
 * 题目描述
 * 大家都知道斐波那契数列，现在要求输入一个整数n，请你输出斐波那契数列的第n项。
 * n<=39
 * </pre>
 *
 */
public class OfferFibonacci {

	public static void main(String[] args) {
//		System.out.println(OfferFibonacci.Fibonacci(0));
//		System.out.println(OfferFibonacci.Fibonacci(1));
//		System.out.println(OfferFibonacci.Fibonacci(2));
//		System.out.println(OfferFibonacci.Fibonacci(3));
//		System.out.println(OfferFibonacci.Fibonacci(4));
//		System.out.println(OfferFibonacci.Fibonacci(5));
//		System.out.println(OfferFibonacci.Fibonacci(6));
//		System.out.println(OfferFibonacci.Fibonacci(7));
//		System.out.println(OfferFibonacci.Fibonacci(8));
		System.out.println(OfferFibonacci.JumpFloorII(1));
		System.out.println(OfferFibonacci.JumpFloorII(2));
		System.out.println(OfferFibonacci.JumpFloorII(3));
		System.out.println(OfferFibonacci.JumpFloorII(4));
		System.out.println(OfferFibonacci.JumpFloorII(5));
		System.out.println(OfferFibonacci.JumpFloorII(6));
		System.out.println(OfferFibonacci.JumpFloorII(7));
		System.out.println(OfferFibonacci.JumpFloorII(8));
	}

	public static int Fibonacci(int n) {
		if (n == 0)
			return 0;
		if (n <= 2)
			return 1;
		int i = 1;
		int j = 1;
		int result = 0;
		for (int k = 2; k < n; k++) {
			result = i + j;
			i = j;
			j = result;
		}
		return result;
	}

	/**
	 * <pre>
	 * 题目描述
	 * 一只青蛙一次可以跳上1级台阶，也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
	 * </pre>
	 * 
	 * @param target
	 * @return
	 */
	public static int JumpFloor(int target) {
		if (target == 1)
			return 1;
		if (target == 2)
			return 2;
		int a = 1;
		int b = 2;
		int c = 3;
		for (int i = 3; i <= target; i++) {
			c = a + b;
			a = b;
			b = c;
		}
		return c;

	}

	/**
	 * <pre>
	 * 题目描述
	 * 一只青蛙一次可以跳上1级台阶，也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
	 * </pre>
	 * 
	 * @param target
	 * @return
	 */
	public static int JumpFloorII(int target) {
		return (int) Math.pow(2, (target-1));
//		if (target == 1)
//			return 1;
//		if (target == 2)
//			return 2;
//		int a = 1;
//		int b = 2;
//		int c = 3;
//		for (int i = 3; i <= target; i++) {
//			c = a + b + 1;
//			a = b;
//			b = c;
//		}
//		return c;
		
	}

}
